package com.example.a2022application.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.example.a2022application.BaseActivity;
import com.example.a2022application.R;
import com.example.a2022application.bean.StudentTaskBean;
import com.example.a2022application.net.Urls;
import com.example.a2022application.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.FileCallback;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Progress;
import com.lzy.okgo.model.Response;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;

public class CorrectTaskActivity extends BaseActivity {
    private ListView listcorrect;
    private String tid;
    ShowListAdapter adapter;
    List<StudentTaskBean.DataBean> tList= new ArrayList();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_correct);
        listcorrect = findViewById(R.id.listcorrect);
        Intent intent = getIntent();
        tid = intent.getStringExtra("tid");

    }

    private void initData() {
        OkGo.<String>post(Urls.SELECTSTUDENTTASK)
                .params("timetable_id",tid)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        StudentTaskBean bean = getGson().fromJson(response.body(),StudentTaskBean.class);
                        if (bean.getCode() == 0){
                            tList = bean.getData();
                            adapter = new ShowListAdapter();
                            listcorrect.setAdapter(adapter);
                        }

                    }
                });
    }

    public class ShowListAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return tList.size();
        }

        @Override
        public Object getItem(int i) {
            return tList.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ShowListAdapter.ViewHolder viewHolder;
            //判断界面是否有缓存
            if(convertView == null){
                viewHolder= new ShowListAdapter.ViewHolder();
                convertView= LinearLayout.inflate(parent.getContext(), R.layout.item_correcttask, null);

                viewHolder.tv_task = convertView.findViewById(R.id.tv_task);
                viewHolder.tv_sname = convertView.findViewById(R.id.tv_sname);
                viewHolder.tv_send = convertView.findViewById(R.id.tv_send);
                viewHolder.ll_task = convertView.findViewById(R.id.ll_task);

                convertView.setTag(viewHolder);
            }else{
                viewHolder=(ShowListAdapter.ViewHolder)convertView.getTag();
            }
            StudentTaskBean.DataBean teacher = (StudentTaskBean.DataBean) tList.get(position);

            viewHolder.tv_task.setText(teacher.getOrigname());
            viewHolder.tv_sname.setText(teacher.getNickname());
            String msg = teacher.getTeachermsg();
            if (!TextUtils.isEmpty(msg)){
                viewHolder.tv_send.setText(msg);
            }else {
                viewHolder.tv_send.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(CorrectTaskActivity.this,AddMessageActivity.class);
                        intent.putExtra("gname","teachermsg");
                        intent.putExtra("objid",teacher.getObj_id());
                        startActivity(intent);

                    }
                });
            }
            viewHolder.ll_task.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    new AlertDialog.Builder(CorrectTaskActivity.this)
                            .setTitle("下载该作业吗？")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    downTask(teacher.getFilename());
                                }

                            })
                            .setNegativeButton("取消", null)
                            .show();
                }
            });


            return convertView;
        }

        private class ViewHolder{
            TextView tv_task;
            TextView tv_sname;
            TextView tv_send;
            LinearLayout ll_task;
        }
    }

    private void downTask(String filename) {
        OkGo.<File>post(Urls.DOWNLOAD)
                .params("fileName",filename)
                .execute(new FileCallback() {
                    @Override
                    public void onSuccess(Response<File> response) {
                        File body = response.body();
                        Log.e("tag","body---"+body);
                        if (body.exists()){
                            ToastUtil.showToast("文件下载成功");
                        }
                    }

                    @Override
                    public void downloadProgress(Progress progress) {
                        float fraction = progress.fraction;  //0-1 之间的一个数
//                        bar.setProgress((int) (fraction*100));//用于进度条
                        System.out.println("===================="+fraction);

                        super.downloadProgress(progress);
                    }
                });

    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }
}
